using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace NET_Client
{
    
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    //------------------------------------//
    //------ Struct for sending ----------//
    //------------------------------------//
    public struct ReciveData
    {

        public int count_player;
        public tplayer[] players;

        public byte[] ToBytes()
        {
            Byte[] bytes = new Byte[Marshal.SizeOf(typeof(ReciveData))];
            GCHandle pinStructure = GCHandle.Alloc(this, GCHandleType.Pinned);
            try
            {
                Marshal.Copy(pinStructure.AddrOfPinnedObject(), bytes, 0, bytes.Length);
                return bytes;
            }
            finally
            {
                pinStructure.Free();
            }
        }

    }
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct SendData
    {
        public tplayer player;
        public byte[] ToBytes()
        {
            Byte[] bytes = new Byte[Marshal.SizeOf(typeof(ReciveData))];
            GCHandle pinStructure = GCHandle.Alloc(this, GCHandleType.Pinned);
            try
            {
                Marshal.Copy(pinStructure.AddrOfPinnedObject(), bytes, 0, bytes.Length);
                return bytes;
            }
            finally
            {
                pinStructure.Free();
            }
        }
    }
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct tplayer
    {
        public Int32 ID_MD5;
        public Int32 x;
        public Int32 y;
        public Int32 bird;
    }

    class NetClient
    {
        public Socket Handle;
        public int rbyte;
        //-----------------------------------------//
        //------ Starting Client ------------------//
        //-----------------------------------------//
        public void Start(string IP_ADDR)
        {
            try
            {
                string IP_ADDRESS = IP_ADDR;
                IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(IP_ADDRESS), 11000);
                Handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp);
                Handle.Connect(ipEndPoint);
                Handle.Blocking = false;
            }
            catch (Exception)
            {
                
            }
        }
        //----------------------------------------//
        public void Send(SendData sd)
        {
            byte[] bytes = new byte[8];
            if (Handle != null)
                if (Handle.Connected)
                {
                    if (Handle.Poll(0, SelectMode.SelectWrite))
                    {

                        Handle.Send(sd.ToBytes());
                    }
                }
        }
        //---------------------------------------//
        public void Receive(out ReciveData rd)
        {

            rd = new ReciveData();
            rd.players = new tplayer[12];
            byte[] bytes = new byte[Marshal.SizeOf(typeof(ReciveData))];
            if (Handle != null)
                if (Handle.Connected)
                    if (Handle.Poll(0, SelectMode.SelectRead))
                    {
                        int bytesRec = Handle.Receive(bytes);
                        rbyte = bytesRec;
                        Marshal.Copy(bytes, 0, (IntPtr)rd.count_player, bytes.Length);
                    }
        }
        //--------------------------------------//
    }
}
